package com.rongshu.widget;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * created by zhengweis on 2019/4/25.
 * description：文字评论页面使用的加载进度条
 */
public class SmartLoadingView extends RelativeLayout {
    protected ImageView mArrowView;
    protected ImageView mProgressView;

    protected ProgressDrawable mProgressDrawable;

    public SmartLoadingView(Context context) {
        super(context);
        init(context);
    }

    public SmartLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SmartLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        DensityUtil density = new DensityUtil();

        LinearLayout layout = new LinearLayout(context);
        layout.setId(android.R.id.widget_frame);
        layout.setOrientation(LinearLayout.HORIZONTAL);

        //进度图片
        RelativeLayout.LayoutParams lpArrow = new RelativeLayout.LayoutParams(density.dip2px(20), density.dip2px(20));
        lpArrow.addRule(CENTER_VERTICAL);
        mArrowView = new ImageView(context);
        layout.addView(mArrowView, lpArrow);

        RelativeLayout.LayoutParams lpProgress = new RelativeLayout.LayoutParams((ViewGroup.LayoutParams) lpArrow);
        lpProgress.addRule(CENTER_VERTICAL);
        mProgressView = new ImageView(context);
        mProgressView.animate().setInterpolator(new LinearInterpolator());
        layout.addView(mProgressView, lpProgress);

        RelativeLayout.LayoutParams lpHeaderLayout = new RelativeLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
        lpHeaderLayout.addRule(CENTER_IN_PARENT);
        addView(layout, lpHeaderLayout);

        mProgressDrawable = new ProgressDrawable();
        mProgressDrawable.setColor(ContextCompat.getColor(context, R.color.color_666666));
        mProgressView.setImageDrawable(mProgressDrawable);
    }

    /**
     * 开始动画
     */
    public void start() {
        mProgressDrawable.start();
    }

    /**
     * 停止动画
     */
    public void stop() {
        mProgressDrawable.stop();
    }
}
